﻿#include "PhysicsList.hh"

#include <stdio.h>
#include <stdlib.h>

#include "G4DecayPhysics.hh"
#include "G4EmExtraPhysics.hh"
#include "G4EmStandardPhysics_option4.hh"
#include "G4HadronElasticPhysics.hh"
#include "G4HadronPhysicsQGSP_BIC_HP.hh"
#include "G4IonBinaryCascadePhysics.hh"
#include "G4IonElasticPhysics.hh"
#include "G4IonPhysics.hh"
#include "G4LossTableManager.hh"
#include "G4NeutronTrackingCut.hh"
#include "G4StoppingPhysics.hh"
#include "G4SystemOfUnits.hh"
#include "G4Threading.hh"
#include "MyMacros.hh"
#include "PhysicsListMessenger.hh"
// #include "G4EmStandardPhysics_option3.hh"
// #include "G4HadronPhysicsQGSP_BIC.hh"
// #include "G4Electron.hh"
// #include "G4EmStandardPhysics.hh"
// #include "G4EmStandardPhysics_option4.hh"
// #include "G4Gamma.hh"
// #include "G4HadronElasticPhysicsHP.hh"
// #include "G4HadronElasticPhysicsXS.hh"
// #include "G4IonPhysicsXS.hh"
// #include "G4ParticleTable.hh"
// #include "G4ParticleTypes.hh"
// #include "G4Positron.hh"
// #include "G4ProcessManager.hh"
// #include "G4Proton.hh"
// #include "G4RadioactiveDecayPhysics.hh"
// #include "G4AutoDelete.hh"

using namespace std;
using namespace CLHEP;

PhysicsList::PhysicsList() : G4VModularPhysicsList(), _fEmPhysicsList(0), _fDecayList(0), _fHadronPhys(0)
{
    // DINFOLN("[PhysicsList] PhysicsList()");

    // given verbose level is set to all physics constructors. 越高越复杂.
    SetVerboseLevel(1);

    G4LossTableManager::Instance();
    defaultCutValue = 0.05 * mm;

    _pMessenger = new PhysicsListMessenger(this);

    _fDecayList = new G4DecayPhysics();
    // _fEmPhysicsList = new G4EmStandardPhysics_option3();
    _fEmPhysicsList = new G4EmStandardPhysics_option4();
}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

PhysicsList::~PhysicsList()
{
    delete _pMessenger;
    delete _fDecayList;
    delete _fEmPhysicsList;
    for (size_t i = 0; i < _fHadronPhys.size(); i++) delete _fHadronPhys[i];
}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

void PhysicsList::ConstructParticle()
{
    MYG4TID(tid);
    DINFOLN("[tid {} PhysicsList] ConstructParticle()", tid);
    _fDecayList->ConstructParticle();
}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

void PhysicsList::ConstructProcess()
{
    MYG4TID(tid);
    DINFOLN("[tid {} PhysicsList] ConstructProcess()", tid);

    AddTransportation();

    _fDecayList->ConstructProcess();
    _fEmPhysicsList->ConstructProcess();

    for (size_t i = 0; i < _fHadronPhys.size(); i++) _fHadronPhys[i]->ConstructProcess();
}

/**
s:Ph/Default/Type = "Geant4_Modular"
sv:Ph/Default/Modules = 6 "g4em-standard_opt4" "g4h-phy_QGSP_BIC_HP" "g4decay" "g4ion-binarycascade" "g4h-elastic_HP" "g4stopping"
d:Ph/Default/EMRangeMin = 100. eV
d:Ph/Default/EMRangeMax = 500. MeV
*/

void PhysicsList::AddPhysicsList(const G4String& name)
{
    static auto fnAddEM = [this]()
    {
        delete _fEmPhysicsList;
        // _fEmPhysicsList = new G4EmStandardPhysics_option3();
        _fEmPhysicsList = new G4EmStandardPhysics_option4();
    };

    if (verboseLevel > 0)
    {
        LINFO("`````````````````````````````````````[PhysicsList/AddPhysicsList] name = {}", name)
    }

    if (name == "emstandard_opt3")
    {
        fnAddEM();
    }
    else if (name == "QGSP_BIC_EMY")
    {
        fnAddEM();

        _fHadronPhys.reserve(10);

        _fHadronPhys.push_back(new G4EmExtraPhysics());
        _fHadronPhys.push_back(new G4HadronElasticPhysics());

        // _fHadronPhys.push_back(new G4HadronPhysicsQGSP_BIC());
        _fHadronPhys.push_back(new G4HadronPhysicsQGSP_BIC_HP());

        _fHadronPhys.push_back(new G4StoppingPhysics());
        _fHadronPhys.push_back(new G4IonElasticPhysics());
        _fHadronPhys.push_back(new G4IonPhysics());
        _fHadronPhys.push_back(new G4NeutronTrackingCut());
        _fHadronPhys.push_back(new G4IonBinaryCascadePhysics());
        _fHadronPhys.shrink_to_fit();
    }
    else
    {
        LINFO("`````````````````````````````````````[PhysicsList/AddPhysicsList] name `{}` is not defined", name)
    }
}

void PhysicsList::SetCuts()
{
    G4VUserPhysicsList::SetCuts();  //将粒子的截断设置为默认
}